package com.unhz.taigu.interface_controllers.contract;

import com.unhz.generator.pojo.Contract;
import com.unhz.generator.pojo.Order;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.generator.utils.JsonResult;
import com.unhz.taigu.service.ContractService;
import com.unhz.taigu.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Author: 沈佳庆
 * @Description: 合同
 * @Date: Created in 下午 6:52 2018/3/3 0003
 * @Modified By: V1.0.0
 */
@RestController
@RequestMapping("/IContract")
public class IContractController{

    @Autowired
    private ContractService contractService;

    @Autowired
    private OrderService orderService;

    /**
     * 委托加工合同_待确认
     * @param model
     * @param orderId
     * @return
     */
    @RequestMapping("/confirmContracting")
    public JsonResult confirmContracting(Model model, @RequestParam(value= "orderId",required = false) Integer orderId){
        List<Contract> contractList = contractService.findContractsByOrderId(orderId);
        Order order = orderService.selectOrderById(orderId);
        if(EmptyUtil.isEmpty(contractList)){
            return JsonResult.failure("查询合同失败!");
        }
        int number = 0;
        for(int i=0;i<contractList.size();i++){
            number = number + contractList.get(i).getNumber();
        }
        model.addAttribute("number",number);
        model.addAttribute("order",order);
        return JsonResult.success("查询合同成功!",model);
    }

    /**
     * 合同确认
     * @param model
     * @param orderId
     * @return
     */
    @RequestMapping("/confirmContract")
    public JsonResult confirmContract(Model model, @RequestParam(value= "orderId",required = false) Integer orderId){
        int count = orderService.confirmContract(orderId);
        if(count>0){
            return JsonResult.success("合同确认成功!");
        }
        return JsonResult.failure("合同确认失败!");
    }


}
